/**
 *
 */

package lohmar.ashar.android.batterySaver.helper;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.location.LocationManager;

import java.util.List;

/**
 * @author Mihai HARAS (Lohmar ASHAR) 2011
 */
public class GpsHelper implements IServiceHelper {

    private ContextWrapper context;

    public GpsHelper(ContextWrapper context) {
        this.context = context;
    }

    /*
     * (non-Javadoc)
     * @see lohmar.ashar.android.batterySaver.helper.IServiceHelper#isServiceEnabled()
     */
    @Override
    public boolean isServiceEnabled() {
        LocationManager mGpsService = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        return mGpsService.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    /*
     * (non-Javadoc)
     * @see lohmar.ashar.android.batterySaver.helper.IServiceHelper#isServicePresent()
     */
    @Override
    public boolean isServicePresent() {
        LocationManager mgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (mgr == null) {
            return false;
        }
        List<String> providers = mgr.getAllProviders();
        if (providers == null) {
            return false;
        }
        return providers.contains(LocationManager.GPS_PROVIDER);
    }

    /*
     * (non-Javadoc)
     * @see lohmar.ashar.android.batterySaver.helper.IServiceHelper#setServiceEnabled(boolean)
     */
    @Override
    public void setServiceEnabled(boolean serviceEnabled) {
        Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
        intent.putExtra("enabled", serviceEnabled);
        context.sendBroadcast(intent);
    }

    @Override
    public String getServiceName() {
        return "gps";
    }

}
